
package jfc.ui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;
import javax.swing.border.Border;


/**
 *  @author Yann Le Gall
 *  ylegall@gmail.com
 *  Oct 25, 2009 4:47:40 PM
 */
public class MainWindow extends JFrame implements ActionListener
{
    private static final MainWindow instance;
    private final GamePanel gamePanel;
    private final JLabel statusLabel, numCardsLabel;

    static {
        instance = new MainWindow();
    }

    public static final MainWindow getInstance() {
        return instance;
    }

    private MainWindow()
    {
        try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {}
        
        Font f = new Font("Verdana", Font.BOLD, 12);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                dispose();
                System.exit(0);
            }} );

        // menu
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");

        String text = "new";
        JMenuItem item = new JMenuItem(text);
        item.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_DOWN_MASK));
        item.setActionCommand(text);
        item.addActionListener(this);
        fileMenu.add(item);

        text = "open";
        item = new JMenuItem(text);
        item.setAccelerator(KeyStroke.getKeyStroke('O', InputEvent.CTRL_DOWN_MASK));
        item.setActionCommand(text);
        item.addActionListener(this);
        fileMenu.add(item);

        text = "save";
        item = new JMenuItem(text);
        item.setAccelerator(KeyStroke.getKeyStroke('S', InputEvent.CTRL_DOWN_MASK));
        item.setActionCommand(text);
        item.addActionListener(this);
        fileMenu.add(item);
        fileMenu.add(new JSeparator());

        text = "quit";
        item = new JMenuItem(text);
        item.setAccelerator(KeyStroke.getKeyStroke('Q', InputEvent.CTRL_DOWN_MASK));
        item.setActionCommand(text);
        item.addActionListener(this);
        fileMenu.add(item);

        menuBar.add(fileMenu);
        
        // edit menu and undo action
        fileMenu = new JMenu("edit");
        text = "undo";
        item = new JMenuItem(text);
        item.setAccelerator(KeyStroke.getKeyStroke('Z', InputEvent.CTRL_DOWN_MASK));
        item.setActionCommand(text);
        item.addActionListener(this);
        fileMenu.add(item);
        menuBar.add(fileMenu);
        
        add(menuBar, BorderLayout.NORTH);

        gamePanel = new GamePanel();
        add(gamePanel, BorderLayout.CENTER);

        // status Label:
        JPanel bottomPanel = new JPanel(new GridLayout(1,2));
        JPanel statusPanel = new JPanel();
        JPanel infoPanel = new JPanel();
        statusLabel = new JLabel(" ");
        statusLabel.setFont(f);
        JLabel remainingLabel = new JLabel("remaining Cards: ");
        numCardsLabel = new JLabel(" ");
        numCardsLabel.setFont(f);
        remainingLabel.setFont(f);
        statusPanel.add(statusLabel, BorderLayout.WEST);
        infoPanel.add(remainingLabel);
        infoPanel.add(numCardsLabel);
        Border b = BorderFactory.createEtchedBorder();
        infoPanel.setBorder(b);
        statusPanel.setBorder(b);
        bottomPanel.add(statusPanel);
        bottomPanel.add(infoPanel);
        add(bottomPanel, BorderLayout.SOUTH);

        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setTitle("Free Cell");
        setVisible(true);
        pack();
    }

    public final void setPanelImage() {
        gamePanel.setImage();
    }

    public final void paintPanel() {
        Graphics2D g2 = (Graphics2D)gamePanel.getGraphics();
        gamePanel.paintPanel(g2);
        g2.dispose();
    }
    
    public final void repaintPanel() {
    	gamePanel.repaint();
    }

    public void setMessage(final String message) {
        new Thread(new Runnable() {
            @Override public void run() {
                statusLabel.setText(message);
                try { Thread.sleep(3000); }
                catch (InterruptedException e) {}
                finally {statusLabel.setText(" ");}
            } }).start();
    }
    
    public final void setRemainingCards(int number) {
    	numCardsLabel.setText(number + "");
    }

    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if(command.equals("quit")) {
            dispose();
            System.exit(0);
        }
        else if (command.equals("new")) {
            gamePanel.newGame(this);
            pack();
            repaint();
        }
        else if (command.equals("open")) {
            
        }
        else if (command.equals("save")) {
            if(gamePanel.save())
                setMessage("saved.");
            else
                setMessage("not saved.");
        }
        else if (command.equals("undo")) {
            gamePanel.undo();
        }
    }
    
}
